package main;

import java.io.FileReader;
import java.io.IOException;
 
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
 
import org.apache.log4j.Logger;
import org.w3c.dom.Element; 
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
 
public class Config
{
	static Logger logger = Logger.getLogger(Game.class);
	
	public static synchronized Config getInstance()
	{
		if (instance == null)
		{
			instance = new Config();
		}
		
		return instance;
	}
	
	public boolean LoadConfigure(String configureFile)
		throws ParserConfigurationException, SAXException, IOException, XPathExpressionException
	{ 
		XPathFactory factory     = XPathFactory.newInstance();
		XPath        xpath 		 = factory.newXPath(); 
		
		String content = xpath.evaluate("//packetQueueSize/text()", 
				                        new InputSource(new FileReader(configureFile))); 
		packetQueueSize = Integer.parseInt(content);
		logger.info("packetQueueSize = " + packetQueueSize); 
		 
		content = xpath.evaluate("//packetThreadSize/text()", 
				                 new InputSource(new FileReader(configureFile))); 
		packetThreadSize = Integer.parseInt(content);
		logger.info("packetThreadSize = " + packetThreadSize); 
		
		content = xpath.evaluate("//useMemcache/text()", 
				                 new InputSource(new FileReader(configureFile))); 
		useMemcache = Boolean.parseBoolean(content);
		logger.info("useMemcache = " + useMemcache);
		
		content = xpath.evaluate("//opLogQueueSize/text()", 
                			      new InputSource(new FileReader(configureFile))); 
		opLogQueueSize = Integer.parseInt(content);
		logger.info("opLogQueueSize = " + opLogQueueSize);
		
		content = xpath.evaluate("//opLogThreadSize/text()", 
			      new InputSource(new FileReader(configureFile))); 
		opLogThreadSize = Integer.parseInt(content);
		logger.info("opLogThreadSize = " + opLogThreadSize);
		
		udpServerHost = xpath.evaluate("//udpserver/host/text()",
				        			   new InputSource(new FileReader(configureFile)));
		logger.info("udpServerHost = " + udpServerHost); 
		
		content = xpath.evaluate("//udpserver/port/text()", 
			      				 new InputSource(new FileReader(configureFile))); 
		udpServerPort = Integer.parseInt(content);
		logger.info("udpServerPort = " + udpServerPort);
		
		content = xpath.evaluate("//udpserver/sendBufferSize/text()", 
 				 				new InputSource(new FileReader(configureFile))); 
		udpServerSendBufferSize = Integer.parseInt(content);
		logger.info("udpServerSendBufferSize = " + udpServerSendBufferSize);

		content = xpath.evaluate("//udpserver/receiveBufferSize/text()", 
			      				 new InputSource(new FileReader(configureFile))); 
		udpServerReceiveBufferSize = Integer.parseInt(content);
		logger.info("udpServerReceiveBufferSize = " + udpServerReceiveBufferSize); 
		
		NodeList providerNodes = (NodeList) xpath.evaluate("//database/dataProvider", 
				                                           new InputSource(new FileReader(configureFile)), 
				                                           XPathConstants.NODESET);
		
		for (int i = 0; i < providerNodes.getLength(); i++)
		{ 
			Element provider = (Element) providerNodes.item(i);
			 
			String name 			= xpath.evaluate("@name", provider); 
			String host 		    = xpath.evaluate("host/text()", provider); 
			int    port 			= Integer.parseInt(xpath.evaluate("port/text()", provider));
			String schema 			= xpath.evaluate("schema/text()", provider);
			String user 			= xpath.evaluate("user/text()", provider);
			String password 		= xpath.evaluate("password/text()", provider);
			int    cacheDBConnCount = Integer.parseInt(xpath.evaluate("cacheDBConnCount/text()", provider));
		}
		
		return true;
	}
	
	public int getPacketQueueSize()
	{
		return packetQueueSize;
	}
	
	public int getPacketThreadSize()
	{
		return packetThreadSize;
	}
	
	public int getOPLogQueueSize()
	{
		return opLogQueueSize; 
	}
	
	public int getOPLogThreadSize()
	{
		return opLogThreadSize;
	}
	
	public boolean getUseMemcache()
	{
		return useMemcache;
	}
	
	public String getUDPServerHost()
	{
		return udpServerHost;
	}
	
	public int getUDPServerPort()
	{
		return udpServerPort;
	}
	
	public int getUDPServerSendBufferSize()
	{
		return udpServerSendBufferSize;
	}
	
	public int getUDPServerReceiveBufferSize()
	{
		return udpServerReceiveBufferSize;
	}
	
	private Config() 
	{
		packetQueueSize   			= 8196;
		packetThreadSize  			= 60;
		useMemcache       			= false;
		opLogQueueSize    			= 4096;
		opLogThreadSize   			= 1;
		udpServerSendBufferSize	  	= 1024 * 8;
		udpServerReceiveBufferSize 	= 1024 * 8;
	};
	
	private static Config instance;
	
	private int     packetQueueSize;
	private int 	packetThreadSize;
	private boolean useMemcache;
	private int     opLogQueueSize;
	private int     opLogThreadSize;
	private String  udpServerHost;
	private int     udpServerPort;
	private int     udpServerSendBufferSize;
	private int     udpServerReceiveBufferSize;
}
